import { Controller, Get, Post, Body, Patch, Param, Delete, Query, UseGuards } from '@nestjs/common';
import { ArticleService } from './article.service';
import { CreateArticleDto } from './dto/create-article.dto';
import {UpdateArticleDto} from './dto/update-article.dto'
import { ApiOperation, ApiTags } from '@nestjs/swagger';
import { PaginationDto } from './dto/pagination.dto';
import { AuthGuard } from '@nestjs/passport';

@Controller('article')
@ApiTags('article')
export class ArticleController {
  constructor(private readonly articleService: ArticleService) {}

  @Post()
  @ApiOperation({ summary: '创建文章' })
  @UseGuards(AuthGuard('jwt'))
  create(@Body() createArticleDto: CreateArticleDto) {
    return this.articleService.create(createArticleDto);
  }

  @Get()
  @ApiOperation({ summary: '查询所有文章' })
  findAll(@Query() paginationDto: PaginationDto) {
    // 确保 page 和 pageSize 有正确的默认值
    const page = paginationDto?.page && paginationDto.page > 0 ? paginationDto.page : 1;
    const pageSize = paginationDto?.pageSize && paginationDto.pageSize > 0 ? paginationDto.pageSize : 10;
    
    return this.articleService.findAll({ page, pageSize });
  }

  @Get(':id')
  @ApiOperation({ summary: '通过id查询文章' })
  findOne(@Param('id') id: string) {
    return this.articleService.findOne(+id);
  }

  @Patch(':id')
  @ApiOperation({ summary: '通过id更新文章' })
  update(@Param('id') id: string, @Body() updateArticleDto: UpdateArticleDto) {
    return this.articleService.update(+id, updateArticleDto);
  }

  @Delete(':id')
  @ApiOperation({ summary: '通过id删除文章' })
  remove(@Param('id') id: string) {
    return this.articleService.remove(+id);
  }
  @Post('/article-by-category-id')
  @ApiOperation({ summary: '通过分类id查询文章' })
  getArticleByCategoryId(@Body() body: any) {
    return this.articleService.getArticleByCategoryId(body.categoryId);
  }
}
